/*
 * jQuery firefly plugin 0.1
 *
 * http://docs.jquery.com/Plugins/firefly
 *
 * Copyright (c) 2010 Dharmveer Motyar
 * http://motyar.blogspot.com
 * http://motyar.com
 *
 * $Id$
 * 
 * licensed under the MIT licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *  
 *
 * Creates a firefly effect.
 *
 * @example $.firefly();
 *
 * @name firefly
 * @type jQuery
 * @cat Plugins/firefly
 */
 (function($) { 
	/*
	 * Plugin defaults 
	 */
	var defaults = {
			images : ['http://3.bp.blogspot.com/-xpb6N91mo1U/UzxSVaqqDfI/AAAAAAAAJtc/gyD1xmzseCg/s1600/flower-round.png', 'http://3.bp.blogspot.com/-Z8c_sskIxao/UzxSVuD5_JI/AAAAAAAAJtk/Hb_ZHZ97nKI/s1600/flower.png',
			'http://4.bp.blogspot.com/-yp92J9nhYMo/UzxScktKIrI/AAAAAAAAJuE/6Dm4Gn4qjYg/s1600/red-flower.png', 'http://1.bp.blogspot.com/-VIXG96TxoT8/UzxSVrfMD3I/AAAAAAAAJtg/YPw54YKzGqk/s1600/leaf.png'],
			total : 10
	};
	

	$.firefly = function(settings) {
			$.firefly.settings = $.extend({}, defaults, settings);
			if($.firefly.preloadImages()){
			for (i = 0; i < $.firefly.settings.total; i++){
				 $.firefly.fly($.firefly.create($.firefly.settings.images[$.firefly.random(($.firefly.settings.images).length)]));
			}
			}
			return;
	};
	
	/*
	 * Public Functions
	 */

	 $.firefly.create = function(img){
					spark = $('<img>').attr({'src' : img}).hide();
					 $('#fireflies').append(spark);
							return spark.css({
								            'position':'absolute',
										    //'z-index': $.firefly.random(20),
											'z-index': '-1',
											top: $.firefly.random(($(window).height()-50)),	//offsets
											left:  $.firefly.random(($(window).width()-20)) //offsets
											}).show();		
	 }
    


$.firefly.fly = function(sp) {
  $(sp).animate({
	  top: $.firefly.random(($(window).height()-50)),	//offsets
	  left: $.firefly.random(($(window).width()-20))
  }, (($.firefly.random(10) + 5) * 1000),function(){  $.firefly.fly(sp) } );
};

$.firefly.preloadImages = function() {
	var preloads = new Object();
	for (i = 0; i < ($.firefly.settings.images).length; i++){  
			preloads[i] = new Image(); preloads[i].src =  $.firefly.settings.images[i];
        }
	return true;
}

$.firefly.random = function(max) {
	return Math.ceil(Math.random() * max) - 1;
}
		
})(jQuery);